package com.cjack.frenchexcel.controller;

import com.alibaba.fastjson.JSONObject;
import com.cjack.frenchexcel.config.AjaxResult;
import com.cjack.frenchexcel.config.CommonConfig;
import com.cjack.frenchexcel.config.LayPage;
import com.cjack.frenchexcel.exception.CommonException;
import com.cjack.frenchexcel.model.AdminUserModel;
import com.cjack.frenchexcel.model.SendGoodsRecordModel;
import com.cjack.frenchexcel.service.GoodsService;
import com.cjack.frenchexcel.service.SendGoodsRecordService;
import com.cjack.frenchexcel.service.UserAdminService;
import com.cjack.frenchexcel.table.AdminUserTable;
import com.cjack.frenchexcel.table.SendGoodsRecordTable;
import com.cjack.frenchexcel.util.CustomerStringUtil;
import com.cjack.frenchexcel.util.ModelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/sendGoodsRecord")
public class SendGoodsRecordEndpoint extends BaseEndpoint{

    @Autowired
    SendGoodsRecordService service;
    @Autowired
    UserAdminService userAdminService;
    @Autowired
    GoodsService goodsService;

    /**
     * 列表
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult list( HttpSession session, LayPage layPage, SendGoodsRecordModel condition) {

        try{
            Object loginUserId = session.getAttribute( CommonConfig.SESSION_NAME);
            condition.setAdminUserId( (Long)loginUserId);
            Page<SendGoodsRecordTable> resultPage = service.list( layPage, condition);
            List<SendGoodsRecordModel> datas = ModelUtils.copyListModel( resultPage.getContent(), SendGoodsRecordModel.class);
            for( SendGoodsRecordModel data : datas){
                AdminUserTable user = userAdminService.get( data.getAdminUserId());

                if( user != null){
                    data.setJobNumber( user.getJobNumber());
                }else{
                    data.setJobNumber( "NOT_FOUND_USER");
                }
                data.setSellTotalAmount( goodsService.sumSellPrice( data.getGoodsIds()));
            }
            layPage.setData( datas);
            layPage.setCount( Long.valueOf( resultPage.getTotalElements()).intValue());
            return AjaxResult.SUCCESS( layPage);
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    @RequestMapping(value = "/sendGoods", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult sendGoods( HttpSession session, SendGoodsRecordModel model) {
        try{
            Object adminUserId = session.getAttribute( CommonConfig.SESSION_NAME);
            model.setAdminUserId( (Long)adminUserId);
            service.sendGoods( model);
            return AjaxResult.SUCCESS();
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    /**
     * 导出excel，分两步走，先生成excel放到对应的目录，再返回生成的excel的地址给浏览器端
     * @param condition
     * @return
     */
    @RequestMapping(value = "/exportAsExcel", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult exportAsExcel( HttpSession session, SendGoodsRecordModel condition) {
        try{
            Object loginUserId = session.getAttribute( CommonConfig.SESSION_NAME);
            condition.setAdminUserId( (Long)loginUserId);
            JSONObject json = service.exportAsExcel( condition);
            //返回重复的数据
            return AjaxResult.SUCCESS( json);
        }catch ( CommonException e) {
            e.printStackTrace();
            return AjaxResult.ERROR( e.getCode(), e.getMessage());
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    /**
     * 导出excel，分两步走，先生成excel放到对应的目录，再返回生成的excel的地址给浏览器端
     * @param condition
     * @return
     */
    @RequestMapping(value = "/exportInvoice", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult exportInvoice( HttpSession session, SendGoodsRecordModel condition) {
        try{
            Object loginUserId = session.getAttribute( CommonConfig.SESSION_NAME);
            condition.setAdminUserId( (Long)loginUserId);
            JSONObject json = service.exportInvoice( condition);
            //返回重复的数据
            return AjaxResult.SUCCESS( json);
        }catch ( CommonException e) {
            e.printStackTrace();
            return AjaxResult.ERROR( e.getCode(), e.getMessage());
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    /**
     * 新增
     * @return
     */
    @RequestMapping(value = "/addOrUpdate", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult addOrUpdate( SendGoodsRecordModel model) {
        try{
            if( isAdd( model.getId())){
                model.setCreateTime( new Date());
            }
            model.setUpdateTime( new Date());
            service.save( model);
            return AjaxResult.SUCCESS();
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }
}
